
//210.课程表II
class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& nums) {
        //动态的拓扑排序

        vector<int> ret;
        unordered_set<int> learned;    //统计已经学习过的课程
        while(ret.size()<numCourses)   //每一次循环进行找一次入度为0的课程
        {
            unordered_set<int> s;
            for(auto& tmp:nums)
                if(!learned.count(tmp[1])) s.insert(tmp[0]);   //有前置条件的课程
            if(s.size()==numCourses-ret.size()) return {};    //如果都有前条件,说明学不成了

            for(int i=0;i<numCourses;i++)
            {
                if(!s.count(i)&&!learned.count(i)) 
                {
                    ret.push_back(i);
                    learned.insert(i);
                }
            }   
        }
        return ret;
    }
};